import csv
import random
from datetime import datetime
from time import strftime
import numpy as np
from scipy.stats import mannwhitneyu
from scipy.stats import scoreatpercentile

'''
find the appropriate result
'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey



#--------------------------------------------------------------------------
#MAIN
if __name__ == "__main__":
    print "begin at " + getCurTime()
    inputCSV = 'C:/_DATA/CancerData/test/Jun08/total.csv'
    data = build_data_list(inputCSV)
    for i in range(0, len(data[0])):
        print np.median(data[:,i]), scoreatpercentile(data[:,i], 25), scoreatpercentile(data[:,i], 50), scoreatpercentile(data[:,i], 75)
    
    #cal mww test
    pvalue = np.zeros((len(data[0]),len(data[0])))
    for i in range(0, len(data[0])):
        for j in range(i+1, len(data[0])):
            a, b = mannwhitneyu(data[:,i], data[:,j])
            pvalue[i,j] = b
            pvalue[j,i] = b
    print pvalue
    np.savetxt('C:/temp2.csv', pvalue, delimiter=',', fmt = '%10.5f')
    

    '''
    # find appropriate result
    mean = np.mean(data, axis=0)
    #print mean
    variance = 0.12
    i = 1
    for item in data:
        flag = 1
        for j in range(0, len(mean)):
            if (item[j] > mean[j] - variance) and(item[j] < mean[j] + variance):
                flag = flag
            else:
                flag = flag * 0
                continue
        if flag > 0:
            print i, item
        i += 1
    '''

